import { http } from "@/utils/http";
import { baseUrlApi } from "@/api/utils";

export type UserResult = {
  /** 头像 */
  avatar: string;
  /** 用户名 */
  username: string;
  /** 昵称 */
  nickname: string;
  /** 业务领域 */
  domainId?: string;
  /** 当前登录用户的角色 */
  roles: Array<string>;
  /** `token` */
  accessToken: string;
  /** 用于调用刷新`accessToken`的接口时所需的`token` */
  refreshToken: string;
  /** `accessToken`的过期时间（格式'xxxx/xx/xx xx:xx:xx'） */
  expires: Date;
};

export type RefreshTokenResult = {
  /** `token` */
  accessToken: string;
  /** 用于调用刷新`accessToken`的接口时所需的`token` */
  refreshToken: string;
  /** `accessToken`的过期时间（格式'xxxx/xx/xx xx:xx:xx'） */
  expires: number;
};

/** 登录 */
export const getLogin = (data?: object) => {
  // return http.request<UserResult>("post", baseUrl("/login"), { data });
  return http.request<UserResult>("post", baseUrlApi("/login"), { data });
};

/** 刷新token */
export const refreshTokenApi = (data?: object) => {
  return http.request<RefreshTokenResult>("post", baseUrlApi("/refreshToken"), {
    data
  });
};

export const switchDomainApi = (newDomain: string) => {
  return http.request<UserResult>(
    "put",
    baseUrlApi("/switchDomain/" + newDomain)
  );
};

export const getLogout = () => {
  return http.request<boolean>("post", baseUrlApi("/logout"));
};
